﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using dhDataAcess.sysDAL;
using dhDataAcess.Model;
using dhDataAcess.dataAcess.sysDataSetTableAdapters;
using dhDataAcess.dataAcess;
using dhDataAcess.baseBLL;

namespace dhSystemBLL.basicData
{
   public  class moduleListBLL
    {
        private moduleListTableAdapter  _moduleListAdapter = null;
        private moduleListTableAdapter   moduleListAdapter
        {
            get
            {
                if (_moduleListAdapter == null) _moduleListAdapter = new moduleListTableAdapter ();
                return _moduleListAdapter;
            }
        }
        /// <summary>
        /// 查询程序模块表
        /// </summary>
        /// <returns></returns>

        public sysDataSet.moduleListDataTable getmoduleListAll()
        {
            try
            {
                return moduleListAdapter.GetDatamoduleListAll();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
        }

        /// <summary>
        /// 根据模块编号查询表
        /// </summary>
        /// <param name="moduleNum"></param>
        /// <returns></returns>

        public sysDataSet.moduleListDataTable getmoduleListAll(string moduleNum)
        {
            try
            {

                return moduleListAdapter.GetDatamoduleListByNum(moduleNum);

            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
        }




        /// <summary>
        /// 插入信息
        /// </summary>
        /// <param name="row"></param>
        /// <returns></returns>

        public int moduleListInsert(moduleListModel row)
        {
            int i = 0;
            SqlConnection conn = dataConn.connOpen();
            SqlTransaction tn = null;
            try
            {
                tn = conn.BeginTransaction();
                moduleListDAL moduleList = new moduleListDAL(tn, conn);
                moduleList.moduleListInsert(row);
                tn.Commit();
                return i;
            }
            catch (SqlException ex)
            {
                tn.Rollback();
                if (ex.Number == 2627)
                {
                    throw new Exception("这个模块编号已经存在");
                }


                throw new Exception(ex.Number.ToString() + ex.Message.ToString());

            }
            catch (Exception ex)
            {
                tn.Rollback();
                throw new Exception(ex.Message.ToString());
            }

            finally
            {
                conn.Close();
            }
        }
        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="row"></param>
        /// <returns></returns>

        public int moduleListUpdate(moduleListModel row)
        {
            int i = 0;
            SqlConnection conn = dataConn.connOpen();
            SqlTransaction tn = null;
            try
            {
                tn = conn.BeginTransaction();
                moduleListDAL moduleList = new moduleListDAL(tn, conn);
                moduleList.moduleListUpdate(row);
                tn.Commit();
                return i;
            }
            catch (Exception ex)
            {
                tn.Rollback();
                throw new Exception(ex.Message.ToString());
            }
            finally
            {
                conn.Close();
            }
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="moduleNum"></param>
        /// <returns></returns>

        public int moduleListDel(string moduleNum)
        {
            int i = 0;
            SqlConnection conn = dataConn.connOpen();
            SqlTransaction tn = null;
            try
            {
                tn = conn.BeginTransaction();
                moduleListDAL moduleList = new moduleListDAL(tn, conn);
                moduleList.moduleListDel(moduleNum);
                tn.Commit();
                return i;
            }
            catch (Exception ex)
            {
                tn.Rollback();
                throw new Exception(ex.Message.ToString());
            }
            finally
            {
                conn.Close();
            }
        }
        /// <summary>
        /// 根据电话返回一个公司名称
        /// </summary>
        /// <param name="moduleNum"></param>
        /// <returns></returns>

        public int moduleListRetName(string moduleNum)
        {
            int i = 0;
            SqlConnection conn = dataConn.connOpen();
            SqlTransaction tn = null;
            try
            {
                tn = conn.BeginTransaction();
                moduleListDAL moduleList = new moduleListDAL(tn, conn);
                moduleList.moduleListretName(moduleNum);
                tn.Commit();
                return i;
            }
            catch (Exception ex)
            {
                tn.Rollback();
                throw new Exception(ex.Message.ToString());
            }
            finally
            {
                conn.Close();
            }
        }
    }
}
